require(['layer','datepickerCN'], function(layer,datepickerCN){

	// 编辑用户
	editUser = function(){
		$('.edit-btn').click(function() {
			var self = $(this),
			url      = self.attr("data-url");
			proc_url = self.attr("proc-url");
			id       = self.attr("data-id");

			layer.open({
			    type: 2,
			    title: '编辑用户信息',
			    shade: 0.5,
			    scrollbar : false,
			    area : ['80%','80%'],
			    content: url+'?id='+id,
			    btn: ['修改','关闭'],
			    yes : function(index, layerDom){
					var childBody = layer.getChildFrame('body', index);
					//iframe窗口
					var iframeWin = window[layerDom.find('iframe')[0]['name']];
					//调用子页面方法
					var form_ajax = $(childBody).find('.nav-tabs').attr('form-data');

					var editData = $(childBody).find('.' + form_ajax).serialize();
					//发布
					$.post(proc_url, editData, function(json){
						if(json.code){
							layer.alert(json.msg,{icon: 1});
							layer.close(index);
							pageReload();
						} else {
							layer.alert(json.msg,{icon: 2});
						}
					});
				}
			}); 

		});
	}

	// 详情
	releaseDetails = function(){
		$('.details-btn').click(function() {
			var self = $(this),
			url = self.attr("data-url");
			id  = self.attr("data-id");

			layer.open({
			  type: 2,
			  title: '订单详情',
			  shade: 0.5,
			  scrollbar : false,
			  area : ['90%','100%'],
			  content: url+'?id='+id,
			  btn: ['关闭'],
			});

		});
	}

	// 发货
	deliverGoods = function(){
		$('.deliver-btn').click(function() {
			var self = $(this),
			order_id = self.attr("data-id");
			url      = self.attr("data-url");
			proc_url = self.attr("proc-url");

			layer.open({
			    type: 2,
			    title: '添加发货单信息',
			    shade: 0.5,
			    scrollbar : false,
			    area : ['80%','40%'],
			    content: url+'?order_id='+order_id,
			    btn: ['添加','关闭'],
			    yes : function(index, layerDom){
					var childBody = layer.getChildFrame('body', index);
					//iframe窗口
					var iframeWin = window[layerDom.find('iframe')[0]['name']];
					//调用子页面方法
					var addData = $(childBody).find('.ajax-form').serialize();
					//发布
					$.post(proc_url, addData, function(json){
						if(json.code){
							layer.alert(json.msg,{icon: 1});
							layer.close(index);
							pageReload(1000);
						} else {
							layer.alert(json.msg,{icon: 2});
						}
					});
				}
			});

		});

	}


	// 删除
	releaseDel = function() {
		$('.delete-btn').click(function(){
			var self = $(this),
				id   = self.attr('data-id'),
				url  = self.attr('data-url');
			layer.msg('确定要删除吗？', {
				time: 0 ,//不自动关闭,
				btn: ['确定', '关闭'],
				yes: function(index){
				    layer.close(index);
						var params = {id: id};
						$.post(url, params, function(json){
							if (json.code) {
								layer.alert(json.msg,{icon: 1});
								self.parent().parent().fadeOut('slow');
							} else {
								layer.alert(json.msg,{icon: 2});
							}
						}, 'json');
				}
			});
		});
	};

	//导出
	exportOrder = function() {
		$('.export-btn').click(function(){
			var self      = $(this),
				url       = self.attr('data-url'),
				down_url  = self.attr('down-url'),
				where     = self.attr('data-where');

			var params = {where: where};

			$.post(url, params, function(json) {
				if (json.code) {
					// alert(json.msg.url);
					document.location.href =(down_url + '/' + json.msg);  
				} else {
					layer.alert("导出失败！",{icon: 2});
				}
			}, 'json');
		});
	};

	// 重置
	reset = function() {
		$('.btn-reset').click(function(){
			var form = $('#search-form');
			form.find('option').each(function(){
				if ($(this).attr("selected")) {
					$(this).removeProp("selected");
				}
			});

			form.find('[name="start"]').val('');
			form.find('[name="stop"]').val('');
			form.find('[name="order_sn"]').val('');
		});
	};


	//日期选择
	datepickerStyle('#search-form','.datepicker-style');


	// 编辑用户
	editUser();

	// 详情
	releaseDetails();

	// 发货
	deliverGoods();


	// 删除
	releaseDel();

	//导出
	exportOrder();

	// 重置
	reset();

});

